#pragma once
#include<iostream>
#include<string>
#include <unistd.h>
#include<functional>
#include"InetAddr.hpp"

class Connection;
class Reactor;

using func_t = std::function<void (Connection *)>;

class Connection
{
    public:
    Connection(int sock)
    :_sock(sock)
    ,_R(nullptr)
    {}
    int Sockfd()
    {
        return _sock;
    }
    void SetEvents(int events)
    {
        _events = events;
    }
    uint32_t Events()
    {
        return _events;
    }
    void Register(func_t recver,func_t sender,func_t excepter)
    {
        _recver = recver;
        _sender = sender;
        _exceptrer = excepter;
    }
    void SetSelf(Reactor *R)
    {
        _R = R;
    }
    void AppendInbuffer(const std::string &buff)//追加到写入缓冲区中
    {
        _inbuffer+=buff;
    }
    void AppendOutbuffer(const std::string &buff)//追加到发送缓冲区中
    {
        _outbuffer +=buff;
    }
    std::string &Inbuffer()//写入缓冲区
    {
        return _inbuffer;
    }
    std::string &Outbuffer()//发送缓冲区
    {
        return _outbuffer;
    }
    bool OutbufferEmptr()//发送缓冲区是否为空
    {
       return _outbuffer.empty();
    }
    void OutbufferReMove(int n)//移除成功发送的数据
    {
        _outbuffer.erase(0,n);
    }
    void Close()
    {
       if(_sock>=0)
            ::close(_sock);
        
    }
    private:
    int _sock;
    std::string _inbuffer;
    std::string _outbuffer;

    InetAddr _addr;

    uint32_t _events;
    public:
    func_t _recver;
    func_t _sender;
    func_t _exceptrer;

    Reactor *_R;
};